package com.ktgj.hotel.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.hotel.domain.dic.HotCity;
import com.ktgj.hotel.domain.vo.*;
import com.ktgj.hotel.domain.rooms.FoodSurrounding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.hotel.domain.dic.DicCity;
import com.ktgj.hotel.service.IDicCityService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 区、县、州Controller
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@ControllerAdvice
@RestController
@RequestMapping("/city")
public class DicCityController extends BaseController
{
    @Autowired
    private IDicCityService dicCityService;

    /**
     * 查询区、县、州列表
     */
    //@PreAuthorize(hasPermi = "hotel:city:list")
    @GetMapping("/list")
    public TableDataInfo list(DicCity dicCity)
    {
        startPage();
        List<DicCity> list = dicCityService.selectDicCityList(dicCity);
        return getDataTable(list);
    }

    @PostMapping("/queryDicCityList")
    public AjaxResult queryDicCityList() {
        return AjaxResult.success(dicCityService.queryDicCityList());
    }

    /**
     * 导出区、县、州列表
     */
    //@PreAuthorize(hasPermi = "hotel:city:export")
    @Log(title = "区、县、州", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DicCity dicCity) throws IOException
    {
        List<DicCity> list = dicCityService.selectDicCityList(dicCity);
        ExcelUtil<DicCity> util = new ExcelUtil<DicCity>(DicCity.class);
        util.exportExcel(response, list, "区、县、州数据");
    }

    /**
     * 获取区、县、州详细信息
     */
    //@PreAuthorize(hasPermi = "hotel:city:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dicCityService.selectDicCityById(id));
    }

    /**
     * 新增区、县、州
     */
    //@PreAuthorize(hasPermi = "hotel:city:add")
    @Log(title = "区、县、州", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DicCity dicCity)
    {
        return toAjax(dicCityService.insertDicCity(dicCity));
    }

    /**
     * 修改区、县、州
     */
    //@PreAuthorize(hasPermi = "hotel:city:edit")
    @Log(title = "区、县、州", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DicCity dicCity)
    {
        return toAjax(dicCityService.updateDicCity(dicCity));
    }

    /**
     * 删除区、县、州
     */
    //@PreAuthorize(hasPermi = "hotel:city:remove")
    @Log(title = "区、县、州", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dicCityService.deleteDicCityByIds(ids));
    }

    /**
     * 查询全部市信息(远程)
     * */
    @PostMapping("/allDicCityList")
    public List<DicCityVo> allDicCityList(){
        List<DicCityVo> dicCityVoList = dicCityService.selectAllDicCityList();
        if (dicCityVoList.size() > 0){
            return dicCityVoList;
        }
        else {
            return null;
        }
    }

    @GetMapping("/queryHotCity")
    public AjaxResult queryHotCity() {
        List<HotCity> hotCities = dicCityService.queryHotCity();
        if (hotCities != null) {
            return AjaxResult.success(hotCities);
        } else {
            return AjaxResult.error("获取信息失败");
        }
    }

    /**
     * 查询酒店周边-景点
     * @param request
     * @return
     */
    @PostMapping("/hotelAttractionsSurrounding")
    public AjaxResult hotelAttractionsSurrounding(@RequestBody SurroundingRequest request) {
        AttractionsSurrounding response = dicCityService.hotelAttractionsSurrounding(request);
        if (response.getStatus() == 0) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("查询错误,请稍后重试!");
        }
    }

    /**
     * 查询酒店周边-美食
     * @param request
     * @return
     */
    @PostMapping("/hotelFoodSurrounding")
    public AjaxResult hotelFoodSurrounding(@RequestBody SurroundingRequest request) {
        FoodSurrounding response = dicCityService.hotelFoodSurrounding(request);
        if (response.getStatus() == 0) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("查询错误,请稍后重试!");
        }
    }

    /**
     * 查询酒店周边-交通
     * @param request
     * @return
     */
    @PostMapping("/hotelTrafficSurrounding")
    public AjaxResult hotelTrafficSurrounding(@RequestBody SurroundingRequest request) {
        TrafficSurrounding response = dicCityService.hotelTrafficSurrounding(request);
        if (response.getStatus() == 0) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("查询错误,请稍后重试!");
        }
    }

    @PostMapping("/cityList")
    public AjaxResult cityList(){
        List<popularCityVo> popularCityVoList;
        try {
            popularCityVoList = dicCityService.cityList();
        } catch (Exception e){
            return AjaxResult.error("查询中出现错误");
        }

        if (popularCityVoList != null && popularCityVoList.size() > 0){
            return AjaxResult.success(popularCityVoList);
        }
        else {
            return AjaxResult.error("查询失败");
        }
    }

    @PostMapping("/letterList")
    public AjaxResult letterList(){
        List<LetterVo> letterVos;
        try {
            letterVos = dicCityService.letterList();
        } catch (Exception e){
            return AjaxResult.error("查询中出现错误");
        }

        if (letterVos != null && letterVos.size() > 0){
            return AjaxResult.success(letterVos);
        }
        else {
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 城市名称查经纬度
     * */
    @PostMapping("/dicCityLngAnfLat")
    public String dicCityLngAnfLat(@RequestBody Map<String, Object> map) {
        if (map.get("cityName") != null && !map.get("cityName").equals("")){
            String cityName = map.get("cityName").toString();
            String cityLngAnfLat;
            try {
                cityLngAnfLat = dicCityService.dicCityLngAnfLat(cityName);
            } catch (Exception e){
                return e.getMessage();
            }
            if (cityLngAnfLat != null && !cityLngAnfLat.equals("")){
                return cityLngAnfLat;
            }
            else {
                return "没有数据";
            }
        }
        else {
            return "缺少必要参数";
        }
    }

    /**
     * 获取城市名称
     * */
    @PostMapping("/selectCityName")
    public DicCity selectCityName(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        DicCity dicCity = dicCityService.selectCityName(id);
        return dicCity;
    }

    /**
     * 获取城市id
     * */
    @PostMapping("/selectCityId")
    public Long selectCityId(@RequestBody Map<String, Object> map){
        String name = map.get("name").toString();
        Long id = dicCityService.selectCityId(name);
        return id;
    }
}
